import logging as log



from pathlib import Path

from jinja2 import Environment, FileSystemLoader, Template

from app.utils import db_utils
from app.utils.config_util import ConfigUtil
from app.utils.inspect_utils import InspectUtils

log.basicConfig(level=log.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


def main(map):
    template_name = map['template_name']
    table_name = map['table_name']
    ouput_path = map['ouput_path']

    # 读取配置
    config_path = Path('config/application.yml').resolve()
    config_util = ConfigUtil(config_path)
    config = config_util.read_config()
    engine = db_utils.get_conneaction(config['database'])
    iu = InspectUtils(engine)

    log.info(iu.tables)
    infos =  iu.get_infos(table_name)
    front_info = {}
    front_info['serach_columns'] = []
    front_info['serach_columns'].append({"camelName":"name","comment":"模板名称","type":"string"})
    infos['front_info'] = front_info
    if "primarkName" in map:
        front_info['primarkName'] = map['primarkName']

    package_info = {}
    infos['package_info'] = package_info
    package = "template"
    package_info['api'] = f"github.com/tiger1103/gfast/v3/api/v1/{package}"
    package_info['service'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/service"
    package_info['dao'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/dao"
    package_info['liberr'] = f"github.com/tiger1103/gfast/v3/library/liberr"
    package_info['libUtils'] = f"github.com/tiger1103/gfast/v3/library/libUtils"
    package_info['consts'] = f"github.com/tiger1103/gfast/v3/internal/app/system/consts"
    package_info['model'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/model"
    package_info['entity'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/model/entity"
    package_info['utils'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/utils"
    package_info['common'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/common"
    package_info['commonDao'] = f"github.com/tiger1103/gfast/v3/internal/app/common/dao"
    package_info['commonEntity'] = f"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
    package_info['commonApi'] = f"github.com/tiger1103/gfast/v3/api/v1/common"
    package_info['consts'] = "github.com/tiger1103/gfast/v3/internal/app/system/consts"
    package_info['do'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/model/do"
    package_info['SystemS'] = f"github.com/tiger1103/gfast/v3/internal/app/system/service"
    # dao internal
    package_info['dao_internal'] = f"github.com/tiger1103/gfast/v3/internal/app/{package}/dao/internal"

    log.info(infos)

    file_info = iu.get_file_info(table_name)
    file_info['package'] = package
    file_info['packageFirstUpper'] = package.capitalize()
    package_info['package'] = package
    package_info['packageFirstUpper'] = package.capitalize()

    template_base_path = Path('template')
    log.info(template_base_path)

    template_path = template_base_path.joinpath(template_name)

    output_path = Path(ouput_path).resolve()
    if not output_path.exists():
        output_path.mkdir(parents=True)
    templates = Environment(loader=FileSystemLoader(template_path),lstrip_blocks=True, trim_blocks=True)
    for template in templates.list_templates():
        tl = templates.get_template(template)
        item_template_path = Path(tl.filename)

        relative_path = Template(str(item_template_path.parent.relative_to(template_path))).render(file_info)
        log.info(relative_path)

        render_str = tl.render(infos)
        parent_output = output_path.joinpath(relative_path)
        if not parent_output.exists():
            parent_output.mkdir(parents=True)
        file_path = parent_output.joinpath(Template(item_template_path.stem).render(file_info))
        if Path(file_path).exists()  and (str(item_template_path.stem) in ["router.go","logic.go"]):
            continue
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(render_str)

